﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using SimpleFileManager.Manager;

namespace SimpleFileManager
{
    public class Folder : FilePanelItem
    {
        private DateTime _lastAccessTime;

        public Folder(string name)
            : base(name,FilePanelItemType.Folder)
        {
        }

        public Folder(string name,IFolderIterator iterator)
            : base(name,FilePanelItemType.Folder,iterator)
        {
        }

        public Folder(string name, DateTime lastAccessTime,IFolderIterator iterator)
            : base(name,FilePanelItemType.Folder,iterator)
        {
            _lastAccessTime = lastAccessTime;
        }

        public override long Size
        {
            get { return 0; }
        }

        public override DateTime LastAccessTime
        {
            get { return _lastAccessTime; }
        }

        public override void Add(IFilePanelItem item)
        {
        }

        public override void Remove(IFilePanelItem item)
        {
        }

        public override void Copy(string path)
        {
            string sourceDirName = Path.Combine(Iterator.DirectoryPath,Name),
                destDirName = Path.Combine(path,Name);
            DirectoryInfo dir = new DirectoryInfo(sourceDirName);
            DirectoryInfo[] dirs = dir.GetDirectories();

            if (!dir.Exists)
            {
                throw new DirectoryNotFoundException(
                    "Source Folder does not exist or could not be found: "
                    + sourceDirName);
            }

            if (!Directory.Exists(destDirName))
            {
                Directory.CreateDirectory(destDirName);
            }

            FileInfo[] files = dir.GetFiles();
            foreach (FileInfo file in files)
            {
                string tempPath = Path.Combine(destDirName, file.Name);
                try
                {
                    file.CopyTo(tempPath, false);
                }
                catch (IOException)
                {
                    throw;
                }
            }
            foreach (DirectoryInfo subdir in dirs)
            {
                string temppath = Path.Combine(destDirName, subdir.Name);
                Copy(temppath);
            }
        }

        public override void Create(string path)
        {
            if (Directory.Exists(Path.Combine(path,Name)))
                throw new Exception("Folder already exists");
            try
            {
                Directory.CreateDirectory(Path.Combine(path, Name));
            }
            catch (Exception)
            {
                throw;
            }
        }

        public override void Delete()
        {
            Directory.Delete(Iterator.DirectoryPath + Name, true);
        }

        public override void Move(string path)
        {
            Copy(path);
            Delete();
        }

        public override void MakeAction(SimpleFileManager.Manager.FileManager.FilePanel filePanel)
        {
            filePanel.ChangeFolder(Path.Combine(Iterator.DirectoryPath,Name));
        }

        public override void Rename(string newName)
        {
            Directory.Move(Path.Combine(Iterator.DirectoryPath,Name),Path.Combine(Iterator.DirectoryPath,newName));
        }
    }
}
